<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column prop="title" label="封面图" width="80">
        <template slot-scope="{ row }">
          <img
            class="cover-img"
            :src="row.fileId ? row.sysAttachment.httpUrl : defaultKnowledgeImg"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="知识点名称"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">
          <span @click="handleManage(row)">{{ row.name }}</span>
          <!-- <el-link @click="handleManage(row)" :underline="false">{{
            row.name
          }}</el-link> -->
        </template>
      </el-table-column>
      <el-table-column prop="eduId" label="思政元素">
        <template slot-scope="scope">
          <el-popover placement="top" width="400" trigger="click">
            <div>{{ educationList(scope.row) }}</div>
            <div slot="reference" class="sheng knowledge-strategy">
              {{ educationList(scope.row) }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="eduTarget" label="思政目标">
        <template slot-scope="scope">
          <el-popover placement="top" width="400" trigger="click">
            <div>{{ scope.row.eduTarget || '--' }}</div>
            <div slot="reference" class="sheng knowledge-strategy">
              {{ scope.row.eduTarget || '--' }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="strategy" label="融入策略">
        <template slot-scope="scope">
          <el-popover placement="top" width="400" trigger="click">
            <div>{{ scope.row.strategy | htmlFormat }}</div>
            <div slot="reference" class="sheng knowledge-strategy">
              {{ scope.row.strategy | htmlFormat }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handleBrowse(row)">
              预览
            </el-button>
            <el-button
              type="primary"
              :disabled="
                $store.getters.curCourse.status == 1 ||
                $store.getters.curCourse.auditStatus == 4
              "
              size="mini"
              @click="handleManage(row)"
            >
              管理
            </el-button>
            <el-button
              type="danger"
              :disabled="
                $store.getters.curCourse.status == 1 ||
                $store.getters.curCourse.auditStatus == 4
              "
              size="mini"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  sysKnowledgeQuery,
  sysKnowledgeDelete,
} from '@/api/admin/courseLibrary/sysKnowledge.js'
import Bus from '@/utils/bus.js'
import webinfo from '@/mixins/webinfo.js'
import { KnowledgeSetInfo } from '../../mixins/KnowledgeSetInfo'
export default {
  mixins: [webinfo, KnowledgeSetInfo],
  data() {
    return {
      type: this.$store.getters.libraryType,
      listQuery: {
        page: 1,
        limit: 10,
        courseId: this.$store.getters.courseId,
      },
      tableData: [],
      pageObj: {},
    }
  },
  mounted() {
    Object.assign(this.listQuery, this.$route.query)
    this.getList()
    Bus.$on('refreshKnowledgeList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshKnowledgeList')
  },
  methods: {
    getList() {
      sysKnowledgeQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //附件管理
    handleManage(row) {
      this.$store.commit('SETKNOLEDGE', row)
      let libraryEnName = this.$store.getters.libraryEnName
      this.$router.push({ name: libraryEnName + '-knowledge-setting' })
    },
    //浏览
    handleBrowse(row) {
      this.$store.commit('SETKNOLEDGE', row)
      let name = 'knowdedge-detail'
      if (this.type == 1) {
        name = 'knowdedge-detail'
      }
      if (this.type == 3) {
        name = 'arms-knowdedge'
      }
      if (this.type == 4) {
        name = 'topic-knowdedge-detail'
      }
      let routeUrl = this.$router.resolve({
        name: name,
        query: { id: row.id, queryMode: 2 },
      })
      window.open(routeUrl.href, '_blank')
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysKnowledgeDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>
<style scoped>
.cover-img {
  display: block;
  width: 60px;
  height: 40px;
  border: 1px solid #eee;
  box-sizing: border-box;
}
.knowledge-strategy {
  max-width: 200px;
}
</style>